---
title: "Untitled"
output: html_document
---
############################################
Title: 3_06_Figure_6.rmd
Purpose: This code creates Figure 6.
Last updated: 25-04-2023

Input:
    - Baseline macro data. Latest version: final_agg_macro_data_monthly.csv 
    - Baseline macro data. Latest version: final_agg_macro_data_quarterly.csv 
 
Output:
    - Figure 6
    
##########################################


0. Setup *Set your file path here*
```{r, results="hide", include=FALSE}
#Clear user defined environment
rm(list=ls())
#Clear memory
#gc()

#Load packages
require(stargazer); require(lubridate); library(sandwich); library(ggplot2); require(dplyr); require(tidyverse); library(readxl); library(sandwich); library(lmtest); library(psych)
require(quantmod); require(dynlm); require(AER); require(vars); require(forecast); require(stargazer); require(strucchange); require(xts); require(lubridate); require(fGarch); require(zoo); require(timeSeries); library(sandwich); require(car);library(fBasics); library(ggplot2); require(dplyr); require(tidyverse); library(readxl); library(sandwich); library(QuantPsyc); library(tseries); library(latticeExtra); library(corrplot); library(psych) ; library(stringr); library(readr); library(see)


#options
options(digits=4)
options(stringsAsFactors = FALSE)
options(scipen=999)

#Assign dplyr verbs  
select <- dplyr::select
rename <- dplyr::rename
mutate <- dplyr::mutate
filter <- dplyr::filter
arrange <- dplyr::arrange
distinct <- dplyr::distinct
group_by <- dplyr::group_by
summarise <- dplyr::summarise
lag <- dplyr::lag


#Define directories
global <- ".../2024_RFS_Replication_TOSUBMIT"


```



2. Quarterly Plots
--------------------------------------------------

2.1 Load
```{r}

#Macro data
final_data <- read_csv(paste0(global, "/Data/final_agg_macro_data_quaterly.csv", ""), 
     col_types = cols(...1 = col_skip(), date = col_date(format = "%d/%m/%Y")))

#Start dataset at 2000-03-01
final_data <- final_data %>% 
  filter(date_m >= "1999-12-01" & date_m <= "2023-03-31")

```

2.2 FSLOSS
```{r, results="hide", include=FALSE}

fsloss <- final_data  %>% 
  filter(date_m > "1999-12-01") %>% 
  ggplot()+
  geom_line(aes( x = date_m, y = agg_elp_6_v3*100 ), colour = "blue")+ 
  geom_line(aes( x = date_m, y = ebp), colour = "red")+
  geom_line(aes( x = date_m, y = fsloss/10), colour = "darkgrey") +
  scale_y_continuous(sec.axis = sec_axis(~.*10, name = "FSLOSS")) +
  xlab("")+
  theme_bw() +
  ylab("% spread") 

```

2.3 NFIB
```{r, results="hide", include=FALSE}

nfib <- final_data  %>% 
  filter(date_m > "1999-12-01") %>% 
  ggplot()+
  geom_line(aes( x = date_m, y = agg_elp_6_v3*100 ), colour = "blue")+ 
  geom_line(aes( x = date_m, y = ebp), colour = "red")+
  geom_line(aes( x = date_m, y = nfib/1), colour = "darkgrey") +
  scale_y_continuous(sec.axis = sec_axis(~.*1, name = "NFIB")) +
  xlab("")+
  theme_bw() +
  ylab("% spread") 



```

2.4 NPL
```{r, results="hide", include=FALSE}

npl <- final_data  %>% 
  filter(date_m > "1999-12-01") %>% 
  ggplot()+
  geom_line(aes( x = date_m, y = agg_elp_6_v3*100 ), colour = "blue")+ 
  geom_line(aes( x = date_m, y = ebp), colour = "red")+
  geom_line(aes( x = date_m, y = npl_ratio/1), colour = "darkgrey") +
  scale_y_continuous(sec.axis = sec_axis(~.*1, name = "NPL")) +
  xlab("")+
  theme_bw() +
  ylab("% spread") 

```

2.5 Equity Ratio
```{r, results="hide", include=FALSE}

equity <- final_data  %>% 
  filter(date_m > "1999-12-01") %>% 
  ggplot()+
  geom_line(aes( x = date_m, y = agg_elp_6_v3*100 ), colour = "blue")+ 
  geom_line(aes( x = date_m, y = ebp), colour = "red")+
  geom_line(aes( x = date_m, y = total_equity_ratio-5), colour = "darkgrey") +
  scale_y_continuous(sec.axis = sec_axis(~.+5, name = "Equity Ratio")) +
  xlab("")+
  theme_bw() +
  ylab("% spread") 


```



3. Monthly Plots
--------------------------------------------------

3.1 Load
```{r}

#Macro data
final_data <- read_csv(paste0(global, "/Processed/final_agg_macro_data_monthly.csv", ""), 
      col_types = cols(...1 = col_skip(), date = col_date(format = "%Y-%m-%d")))


#Start dataset at 2000-03-01
final_data <- final_data %>% 
  filter(date >= "1999-12-01" & date <= "2023-03-31")

```

3.2 FacilityNum_3m_ma 
```{r, results="hide", include=FALSE}

facilitynum <- final_data  %>% 
  filter(date > "1999-12-01") %>% 
  ggplot()+
  geom_line(aes( x = date, y = agg_elp_6_v3*100 ), colour = "blue")+ 
  geom_line(aes( x = date, y = ebp), colour = "red")+
  geom_line(aes( x = date, y = FacilityNum_3m_ma/50), colour = "darkgrey") +
  scale_y_continuous(sec.axis = sec_axis(~.*50, name = "Facility Num")) +
  xlab("")+
  theme_bw() +
  ylab("% spread") 

```

3.3 FacilityAmt_3m_ma
```{r, results="hide", include=FALSE}

facilityamt <-  final_data  %>% 
  filter(date > "1999-12-01") %>% 
  ggplot()+
  geom_line(aes( x = date, y = agg_elp_6_v3*100 ), colour = "blue")+ 
  geom_line(aes( x = date, y = ebp), colour = "red")+
  geom_line(aes( x = date, y = FacilityAmt_3m_ma/10000), colour = "darkgrey") +
  scale_y_continuous(sec.axis = sec_axis(~.*10000, name = "Facility Amt")) +
  xlab("")+
  theme_bw() +
  ylab("% spread") 

```



4 Combined plot
-----------------------------------------------

4.1 Figure 6
```{r, results="hide", include=FALSE}

library(ggpubr)

ggarrange(fsloss, nfib, facilityamt, facilitynum, npl, equity,  nrow = 3 , ncol = 2,  align = "v")



```

